var CryptoJS = require("crypto-js"); //引用AES源码js
var dayjs = require("dayjs");
import md5 from "js-md5";
var key = CryptoJS.enc.Utf8.parse("2f48eccf22c9d2d5"); //十六位十六进制数作为密钥
var iv = CryptoJS.enc.Utf8.parse("7529edbaf4617265"); //十六位十六进制数作为密钥偏移量
//解密方法
export function decrypt(word) {
    var decrypt = CryptoJS.AES.decrypt(word, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    // console.log('yulan-解密参数',word,'decrypt:',decrypt, '返回：', CryptoJS.enc.Utf8.stringify(decrypt))
    return CryptoJS.enc.Utf8.stringify(decrypt);
}
//加密方法
export function encrypt(word) {
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
/**
 *
 * @param url 要拼接的URL
 * @param secret 接口返回的密钥
 * @param expiredTime 过期时间（毫秒） 默认为1天
 * @returns 加密过后的url链接
 */
export var sign = function (url, secret, expiredTime) {
    if (expiredTime === void 0) { expiredTime = 86400000; }
    var timestamp = dayjs()
        .add(expiredTime)
        .valueOf();
    var random = Math.round(Math.random() * 100);
    var uid = 0;
    var u;
    try {
        u = parseUrl(url);
    }
    catch (error) {
        throw "URL格式错误";
    }
    var path = u.pathname;
    var sign = path + "-" + timestamp + "-" + random + "-" + uid + "-" + secret;
    var _md5 = md5(sign);
    var authKey = timestamp + "-" + random + "-" + uid + "-" + _md5;
    return url + (url.includes("?") ? "&" : "?") + "auth_key=" + authKey;
};
/**
 * 通过url链接解析出参数
 * @param url url链接
 * @returns 路由参数
 */
export var parseUrl = function (url) {
    var result = {};
    var keys = [
        "href",
        "origin",
        "protocol",
        "host",
        "hostname",
        "port",
        "pathname",
        "search",
        "hash"
    ];
    var i;
    var regexp = /(([^:]+:)\/\/(([^:\/\?#]+)(:\d+)?))(\/[^?#]*)?(\?[^#]*)?(#.*)?/;
    var match = regexp.exec(url);
    if (match) {
        for (i = keys.length - 1; i >= 0; --i) {
            result[keys[i]] = match[i] ? match[i] : "";
        }
    }
    return result;
};
